# -*- coding: utf-8 -*-
# function library for RunGAME
# other functions are imported from other files
from math import floor, ceil
from random import choice as randchoice
from random import randint
from json import load as jsonload
from json import dump as jsondump
from datetime import datetime
datetime
# 32-bit integer to bytes
def IntToBytes(num: int) -> bytes:
    temp_str = bin(num).replace("0b", "")
    if temp_str[0] == '-':
        signed = '1'
        temp_str = temp_str[1:]
    else:
        signed = '0'
    temp_str = signed + ('0' * (31-len(temp_str)) + temp_str)
    temp_ls = [flag=='1' for flag in temp_str]
    ttple: list = []
    for i in range(4):
        t = temp_ls[i*8:i*8+8]
        temp_num = 0
        for j in range(8):
            temp_num = temp_num << 1 | t[j]
        ttple.append(temp_num)
    return bytes(ttple)

# bytes to 32-bit integer
def BytesToInt(byte: bytes) -> int:
    signed: bool = bool(byte[0] & (1 << 7))
    temp_byte0: int = byte[0] & ((1 << 7) - 1)
    tmp = [temp_byte0]
    for i in range(1,4):
        tmp.append(byte[i])
    res: int = 0
    for i in tmp:
        res = (res << 8) | i
    if signed:
        res = -res
    return res

# pygame rectangles touching each other
def RectNext(Rect1, Rect2) -> bool:
    return (
            # Rect1 above Rect2
            Rect1.right == Rect2.left
            and Rect1.top < Rect2.bottom
            and Rect1.bottom > Rect2.top
    ) or (
            # Rect1 below Rect2
            Rect1.left == Rect2.right
            and Rect1.top < Rect2.bottom
            and Rect1.bottom > Rect2.top
    ) or (
            # Rect1 left of Rect2
            Rect1.bottom == Rect2.top
            and Rect1.left < Rect2.right
            and Rect1.right > Rect2.left
    ) or (
            # Rect1 right of Rect2
            Rect1.top == Rect2.bottom
            and Rect1.left < Rect2.right
            and Rect1.right > Rect2.left
    )

def ConsoleMess(msg: str) -> None:
    print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}")